<?php

namespace JLPay\Models\Trans\Dto;

/**
 * 商品数据DTO
 * 
 * @author liaochangyi
 * @since 2025-01-25
 */
class GoodsDataDto
{
    protected ?string $costPrice = null;  // 订单原价
    protected ?string $receiptId = null;  // 商品小票ID
    protected ?array $goodsDetail = null;  // 单品列表

    /**
     * 构造函数
     *
     * @param array|null $goodsDataDict 商品数据字典
     */
    public function __construct(?array $goodsDataDict = null)
    {
        if ($goodsDataDict) {
            $this->costPrice = $goodsDataDict['cost_price'] ?? null;
            $this->receiptId = $goodsDataDict['receipt_id'] ?? null;
            
            $goodsDetailData = $goodsDataDict['goods_detail'] ?? [];
            if ($goodsDetailData && is_array($goodsDetailData)) {
                $this->goodsDetail = [];
                foreach ($goodsDetailData as $detail) {
                    $this->goodsDetail[] = new GoodsDetailDto($detail);
                }
            }
        }
    }

    /**
     * 获取订单原价
     *
     * @return string|null
     */
    public function getCostPrice(): ?string
    {
        return $this->costPrice;
    }

    /**
     * 设置订单原价
     *
     * @param string|null $costPrice
     * @return self
     */
    public function setCostPrice(?string $costPrice): self
    {
        $this->costPrice = $costPrice;
        return $this;
    }

    /**
     * 获取商品小票ID
     *
     * @return string|null
     */
    public function getReceiptId(): ?string
    {
        return $this->receiptId;
    }

    /**
     * 设置商品小票ID
     *
     * @param string|null $receiptId
     * @return self
     */
    public function setReceiptId(?string $receiptId): self
    {
        $this->receiptId = $receiptId;
        return $this;
    }

    /**
     * 获取商品详情列表
     *
     * @return GoodsDetailDto[]|null
     */
    public function getGoodsDetail(): ?array
    {
        return $this->goodsDetail;
    }

    /**
     * 设置商品详情列表
     *
     * @param array|null $goodsDetail 商品详情列表，可以是字典数组或GoodsDetailDto对象数组
     * @return self
     */
    public function setGoodsDetail(?array $goodsDetail): self
    {
        if ($goodsDetail) {
            $this->goodsDetail = [];
            foreach ($goodsDetail as $detail) {
                if (is_array($detail)) {
                    $this->goodsDetail[] = new GoodsDetailDto($detail);
                } elseif ($detail instanceof GoodsDetailDto) {
                    $this->goodsDetail[] = $detail;
                } else {
                    // 如果不是数组也不是GoodsDetailDto对象，尝试转换为GoodsDetailDto
                    $this->goodsDetail[] = new GoodsDetailDto($detail);
                }
            }
        } else {
            $this->goodsDetail = null;
        }
        return $this;
    }

    /**
     * 转换为数组格式
     *
     * @return array
     */
    public function toArray(): array
    {
        $goodsDetailArray = [];
        if ($this->goodsDetail) {
            foreach ($this->goodsDetail as $detail) {
                $goodsDetailArray[] = $detail->toArray();
            }
        }

        return [
            'cost_price' => $this->costPrice,
            'receipt_id' => $this->receiptId,
            'goods_detail' => $goodsDetailArray
        ];
    }
} 